package com.example.graph.web;


import com.example.graph.entity.Know;
import com.example.graph.entity.UserNode;
import com.example.graph.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

/**
 * @ClassName: UserController
 * @Description:
 * @Author: keke
 * @Date: 2021/6/7 11:30
 */
@RestController
@RequestMapping(value = "user", produces = MediaType.APPLICATION_JSON_VALUE)
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * @Author keke
     * @Description 添加节点
     * @Date 2021/6/7
     * @Param [userNode]
     * @Return com.example.neodemo.model.UserNode
     **/
    @RequestMapping(path = "/add", method = RequestMethod.POST)
    public UserNode addUserNode(@RequestBody UserNode userNode) {
        return userService.create(userNode);
    }

    /**
     * @Author keke
     * @Description 根据id删除
     * @Date 2021/6/7
     * @Param [id]
     * @Return int
     **/
    @RequestMapping(path = "/delete", method = RequestMethod.POST)
    public int delUserNodeById(@RequestParam(value = "id") long id) {
        userService.deleteById(id);
        System.out.println(id);
        return 1;
    }
    @RequestMapping(path = "/delUser", method = RequestMethod.POST)
    public int delUser(@RequestParam(value = "id") long id) {
        UserNode userNode=new UserNode();

        userService.deleteById(id);
        System.out.println(id);
        return 1;
    }

    /**
     * @Author keke
     * @Description 根据id更新
     * @Date 2021/6/7
     * @Param [userNode]
     * @Return com.example.neodemo.model.UserNode
     **/
    @RequestMapping(path = "/update", method = RequestMethod.POST)
    public UserNode updateUserNodeByNode(@RequestBody UserNode userNode) {
        return userService.updateByNode(userNode);
    }

    /**
     * @Author keke
     * @Description 根据id查询单个节点
     * @Date 2021/6/7
     * @Param [id]
     * @Return com.example.neodemo.model.UserNode
     **/
    @RequestMapping(path = "/get", method = RequestMethod.GET)
    public UserNode getUserNodeById(@RequestParam(value = "id") long id) {
        Optional<UserNode> optionalUserNode = userService.findById(id);
        if (optionalUserNode.isPresent()) {
            return optionalUserNode.get();
        } else {
            return null;
        }
    }

    /**
     * @Author keke
     * @Description 查找所有节点
     * @Date 2021/6/7
     * @Param []
     * @Return java.util.List<com.example.neodemo.model.UserNode>
     **/
    @RequestMapping(path = "/list", method = RequestMethod.GET)
    public List<UserNode> getUserNodeList() {
        return userService.findAll();
    }

    /**
     * @Author keke
     * @Description 给已有的两个节点建立联系
     * @Date 2021/6/7
     * @Param [fromId, toId]
     * @Return com.example.neodemo.model.Know
     **/
    @RequestMapping(path = "/addKnows", method = RequestMethod.POST)
    public Know addKnowsById(@RequestParam(value = "from") long fromId, @RequestParam(value = "to") long toId) {
        Optional<UserNode> fromOpt = userService.findById(fromId);
        Optional<UserNode> toOpt = userService.findById(toId);
        if (fromOpt.isPresent()&&toOpt.isPresent()) {
            return userService.addIKnows(fromOpt.get(),toOpt.get());
        } else {
            return null;
        }
    }

    /**
     * @Author keke
     * @Description 删除两个节点之间的关系
     * @Date 2021/6/7
     * @Param [fromId, toId]
     * @Return java.lang.String
     **/
    @RequestMapping(path = "/delKnows", method = RequestMethod.POST)
    public String deleteKnowsByNodeId(@RequestParam(value = "from") long fromId, @RequestParam(value = "to") long toId) {
        Optional<UserNode> fromOpt =  userService.findById(fromId);
        Optional<UserNode> toOpt =  userService.findById(toId);
        if (fromOpt.isPresent()&&toOpt.isPresent()) {
            userService.deleteKnowByNodeId(fromId,toId);
            return "ok";
        } else {
            return "false";
        }
    }

    @RequestMapping(path = "/addUserNode1", method = RequestMethod.POST)
    public UserNode addUserNode1() {
        UserNode userNode;
        UserNode userNode1;
        UserNode userNode2;
        userNode1=new UserNode();
       // userNode.setNodeId(0L);
        userNode1.setAge(+10);
        userNode1.setName("tom"+0);
        userNode1= userService.create(userNode1);
        return getUserNode(userNode1);
    }

    private synchronized UserNode getUserNode(UserNode userNode1) {
        UserNode userNode;
        UserNode userNode2;
        for (int i = 0; i <1000 ; i++) {
            userNode=new UserNode();
          //  userNode.setNodeId((long) i);
            userNode.setAge(i);
            userNode.setName("tom"+i);
            userNode2 = getUserNode(userNode, userNode1);
            userNode1 =userNode2;
            System.out.println(i);

        }

        return userNode1;
    }

    private synchronized UserNode getUserNode(UserNode userNode, UserNode userNode1) {
        UserNode userNode2;
        userNode2= userService.create(userNode);
        userService.addIKnows(userNode1,userNode2);
        return userNode2;
    }

    @RequestMapping(path = "/addUserNode25", method = RequestMethod.POST)
    public UserNode addUserNode25() {
        UserNode userNode;
        UserNode userNode1;
        userNode=new UserNode();
       // userNode.setNodeId(0L);
        userNode.setAge(10);
        userNode.setName("tom"+0);
        userNode.setName(Math.ceil(Math.random()*100)+"");
        userNode1= userService.create(userNode);


        return userNode1;
    }
    @RequestMapping(path = "deleteAll", method = RequestMethod.POST)
    public int deleteAll() {
        userService.deleteAll();

        return 1;
    }
}
